// 封装获取表格数据的方法
export function getData (root, url, params, arr = []) {
  root.service.get(url, { params: params } || {})
    .then(res => {
      if (res.data.status === 200) {
        root.tableData = res.data.data
        root.total = res.data.total
        root.tableData.map(item => {
          // 可以优化 ...... 思考思考
          arr.map(aItem => {
            if (aItem === 'sex') {
              return ((item[aItem] === 1) || (item[aItem] === '1') ?
                item[aItem + '_text'] = '男'
                : item[aItem + '_text'] = '女')
            } else if (aItem === 'state') {
              return item[aItem] === "1"
                ? (item[aItem + '_text'] = "已入学")
                : item[aItem] === "2"
                  ? (item[aItem + '_text'] = "未入学")
                  : (item[aItem + '_text'] = "休学中")
            } else if (aItem === 'completed') {
              return (item[aItem] ? item[aItem + '_text'] = '是'
                : item[aItem + '_text'] = '否')
            } else if (aItem === 'sexInfo') {
              return (item[aItem] === '1' ? item[aItem + '_text'] = '男'
                : item[aItem + '_text'] = '女')
            }
          })
        })
        root.loading = false
      }
    })
    .catch(err => {
      throw err
    })
}

// 新增和修改方法的封装
import qs from 'qs'
export function changeInfo (root, method, url, form, callback) {
  let data = qs.stringify(form)
  root.service[method](url, data)
    .then(res => {
      if (res.data.status === 200) {
        callback(root, url)
        root.dialogFormVisible = false
        root.$message({ type: 'success', message: res.data.message })
      }
    }).catch(err => {
      throw err
    })
}

// 删除方法的封装
export function delData (root, url, id, callback) {
  root.$alert("确定要删除吗？", "提示", {
    confirmButtonText: '确定',
    callback: () => {
      root.service.delete(`${url}/${id}`).then(res => {
        if (res.status === 200) {
          callback(root, url)
          root.$message({ message: res.data.message, type: "success" })
        }
      })
    }
  }).catch(err => {
    throw err
  })
}